package com.bdqn.day0915.service.impl;

import com.bdqn.day0915.entity.User;
import com.bdqn.day0915.service.UserService;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:Corday
 * @Date:2021/09/15/10:45
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource//数据库操作对象
    private JdbcTemplate jdbcTemplate;
    public User findUser(String username,String password){
        //定义一个sql语句
        String sql="select * from user where username=? and password=?";
//        记录行转换工具，将一条记录转换为实体类的对象
        RowMapper<User> userMapper= new BeanPropertyRowMapper<User>(User.class);
        User user=jdbcTemplate.queryForObject(sql,userMapper,new Object[]{username,password} );
        return user;
    }
    @Override
    public List<User> findAllUsers() {
//        数据库操作
          //jdbcTemplate.
        String sql="select * from user";
        RowMapper<User> userMapper=new BeanPropertyRowMapper<User>(User.class);
        List<User> list=jdbcTemplate.query(sql,userMapper);
        return list;
    }
}
